<template>
    <div style="display: inline">
        <el-button :type="buttonType" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        <el-drawer :visible.sync="show" :close-on-press-escape="false" :title="`${form.id ? '修改' : '新增'}品种`"
            custom-class="employee-store-drawer" modal-append-to-body append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="130">
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="form.sort" type="number" placeholder="请输入排序" />
                </el-form-item>
                <el-form-item label="品种名称" prop="title">
                    <el-input v-model="form.title" placeholder="请输入品种名称" />
                </el-form-item>
                <el-form-item label="banner图">
                    <ml-upload-image :url.sync="form.img" />
                </el-form-item>
                <el-form-item label="状态">
                    <el-switch v-model="form.state" :active-value="1" :inactive-value="2" @change="updataState">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div class="footer">
                <el-button @click="show = false">取 消</el-button>
                <el-button @click="form.id ? handleUpdata() : handleSubmit()">确 定</el-button>
            </div>
        </el-drawer>
    </div>
</template>
<script>
import MlUploadImage from '@/components/MlUploadImg/single.vue'
const Model = {
    // id: null,
    title: "",//true	string		名称
    img: "",	//true	string		分类图
    sort: "",	//true	int		排序
    state: 1,	//true	int		状态 0 全部 1 启用 2 禁用
}

export default {
    name: 'addNest',
    components: { MlUploadImage },
    props: {
        buttonType: {
            type: String,
            default() {
                return 'primary'
            }
        }
    },
    data() {
        return {
            rules: {
                title: [
                    { required: true, message: '请输入品种名称', trigger: 'blur' },
                ],
                sort: [
                    { required: true, message: '请输入排序', trigger: 'blur' },
                ],
            },
            show: false,
            form: { ...{}, ...Model },
        }
    },
    methods: {
        updataState(e) {
            this.form.state = e
        },
        handleEdit: function (obj) {
            this.form = { ...{}, ...obj }
            this.show = true
        },
        handleUpdata: function () {
            this.$refs.form.validate(async (valid) => {
                if (valid) {
                    let res = await this.$request({
                        url: '/admin/pet.category/edit',
                        method: 'post',
                        data: this.form
                    })
                    if (res.code == 200) {
                        this.show = false
                        this.$message.success('操作成功')
                        this.$emit('success')
                    }

                } else {
                    return false
                }
            })
        },
        handleAdd: function () {
            this.form = { ...{}, ...Model }
            this.show = true
        },
        handleSubmit: function () {
            this.$refs.form.validate(async (valid) => {
                if (valid) {
                    let res = await this.$request({
                        url: '/admin/pet.category/add',
                        method: 'post',
                        data: this.form
                    })
                    if (res.code == 200) {
                        this.show = false
                        this.$message.success('操作成功')
                        this.$emit('success')
                    }

                } else {
                    return false
                }
            })
        }
    }
}

</script>
<style lang="scss">
.employee-store-drawer {
    width: 600px !important;

    .el-drawer__title {
        border-top-left-radius: 10px !important;
    }

    .el-drawer__header {
        font-size: 16px !important;
        color: #333333 !important;
        font-weight: 700;
        text-align: center;
    }

    .el-drawer__body {
        border-bottom-left-radius: 10px !important;
        padding: 0 20px;
        overflow: scroll !important;
        padding-bottom: 50px;
    }

    .footer {
        position: absolute;
        width: 100%;
        bottom: 0;
        right: 0;
        height: 66px;
        opacity: 1;
        background: #fff;
        box-shadow: 0 -3px 6px 0 rgba(0, 0, 0, .04), 0 2px 4px 0 rgba(0, 0, 0, .5);
        display: flex;
        align-items: center;
        text-align: center;
        justify-content: center;
        z-index: 99;
    }

    .el-form-item {
        display: block;
        margin-right: 0;
        margin-bottom: 24px !important;
    }

    .el-form-item__label {
        width: 150px;
    }

    .el-form-item__content {
        width: calc(100% - 204px);
        display: inline-block;
    }

}
</style>